<?php

/**
 * Created by PhpStorm.
 * User: GadflyBSD
 * Date: 16/4/25
 * Time: 17:15
 */
class AppModel extends PublicModel{
	/**
	 * 发送电信能力平台短信验证码模型
	 * @param        $mobile
	 * @param string $action
	 * @param int    $uid
	 *
	 * @return array
	 */
	public function sendSms($mobile, $action='register', $uid=0){
		Vendor('CtcSms');
		$sms = new CtcSms();
		return $sms->send($mobile, $uid, $action);
	}

	/**
	 * APP端用户注册模型
	 * @param $data
	 *
	 * @return array
	 */
	public function register($data){
		if(!$this->validator('require', $data['nickname']))
			return array('errcode' => '70001');
		if(!$this->validator('require', $data['mobile']))
			return array('errcode' => '70002');
		if(!$this->validator('mobile', $data['mobile']))
			return array('errcode' => '70003');
		if(!$this->validator('unique', $data['mobile'], array('table' => 'AppUser', 'field' => 'mobile')))
			return array('errcode' => '70004');
		if(!$this->validator('require', $data['sms_code']))
			return array('errcode' => '70005');
		if(!$this->validator('require', $data['password']))
			return array('errcode' => '70006');
		if(!$this->validator('require', $data['repassword']))
			return array('errcode' => '70007');
		if(!$this->validator('notequal', $data['password'], $data['repassword']))
			return array('errcode' => '70008');
		if(!$data['toggle'])
			return array('errcode' => '70009');
		if(!$this->validator('smsCode', $data))
			return array('errcode' => '70011');
		$data['halt'] = randstr();
		$data['clear_key'] = $data['password'];
		$data['password'] = md5(md5($data['password']).$data);
		$data['last_recip'] = get_client_ip();
		return $this->curd(array(
			'type'  => 'add',
			'model' => M('AppUser'),
			'data'  => $data,
		));
	}

	/**
	 * APP端用户登录模型
	 * @param $data
	 */
	public function login($data){
		if(!$this->validator('require', $data['mobile']))
			return array('errcode' => '70002');
		if(!$this->validator('mobile', $data['mobile']))
			return array('errcode' => '70003');
		if(!$this->validator('require', $data['password']))
			return array('errcode' => '70006');
		if(!$this->validator('verifyImg', $data['vdcode']))
			return array('errcode' => '70012');
		if($this->validator('unique', $data['mobile'], array('table' => 'AppUser', 'field' => 'mobile')))
			return array('errcode' => '70014');
		$halt = M('AppUser')->where('mobile="'.$data['mobile'].'"')->getField('halt');
		$where = array('mobile' => $data['mobile'], 'password' => md5(md5($data['password']).$halt));
		if(M('AppUser')->where($where)->count() == 0){
			return array('errcode' => '70014');
		}else{
			$sess = M('AppUser')->where($where)->find();
			session('AppUser', $sess);
			return array('errcode' => 0, 'content' => '用户成功登录汪卡系统!', 'data' => $sess);
		}
	}

	/**
	 * APP端用户退出登录系统模型
	 * @return array
	 */
	public function logout(){
		session('AppUser', null);
		return array('errcode' => 0, 'content' => '用户成功退出汪卡系统!');
	}

	/**
	 * 获得当前用户的SESSION模型
	 * @param bool $refresh     是否刷新SESSION
	 *
	 * @return array
	 */
	/*public function getSession($refresh=false){
		$user = session('AppUser');
		if($refresh){
			session('AppUser', NULL);
			session('AppUser', M('AppUser')->where('uid='.$user['uid'])->find());
		}
		$school = $this->getNgOptions('School');
		return array('errcode' => '0', 'data' => session('AppUser'), 'school' => $school);
	}*/

	public function getSurvey($param){
		return $this->curd(array(
			'model' => M('WakaBindView'),
			'where' => 'role="survey" AND schoolname="'.$param.'"',
			'type'  => 'select',
			'field' => 'nickname, survey_id',
		));
	}

	public function bindWeixin($data){
		$login = $this->login($data);
		if($login['errcode'] == 0){

		}
	}

	public function setRealname($data){
		$validator = $this->validator(array(
			array('type' => 'require', 'value' => $data['realname'], 'errcode' => '70015'),
			array('type' => 'realname', 'value' => $data['realname'], 'errcode' => '70016'),
			array('type' => 'require', 'value' => $data['idcard'], 'errcode' => '70017'),
			array('type' => 'idcard', 'value' => $data['idcard'], 'errcode' => '70018'),
			array('type' => 'require', 'value' => $data['school'], 'errcode' => '70019'),
			array('type' => 'require', 'value' => $data['s_time'], 'errcode' => '70020'),
			array('type' => 'require', 'value' => $data['educational'], 'errcode' => '70021'),
			array('type' => 'require', 'value' => $data['subject'], 'errcode' => '70022'),
			array('type' => 'require', 'value' => $data['student_card'], 'errcode' => '70023'),
			array('type' => 'require', 'value' => $data['dormitory'], 'errcode' => '70024'),
			array('type' => 'require', 'value' => $data['survey'], 'errcode' => '70025'),
			array('type' => 'require', 'value' => $data['parent_tel'], 'errcode' => '70026'),
			array('type' => 'mobile', 'value' => $data['parent_tel'], 'errcode' => '70027'),
			array('type' => 'require', 'value' => $data['idcard1'], 'errcode' => '70028'),
			array('type' => 'require', 'value' => $data['idcard2'], 'errcode' => '70029'),
			array('type' => 'require', 'value' => $data['xscard'], 'errcode' => '70030'),
		));
		if($validator['errcode'] != 0)
			return $validator;
		$add = $this->curd(array(
			'type'  => 'add',
			'model' => M('Realname'),
			'data'  => $data,
		));
		if($add['errcode'] == 0)
			return array('errcode' => '0', 'content' => '系统已受理您的实名认证申请, 请等待审核');
		else
			return $add;
	}
}